package com.fc.redis.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @program: fc-redis->MultipartResolverConfig
 * @description:
 * @author: G_Y
 * @create: 2019-09-06 12:10
 **/
@Slf4j
@Configuration
public class MultipartResolverConfig implements WebMvcConfigurer {

    /**
     * 实现了 MultipartResolver接口常用子类有两个：
     * 一个是  CommonsMultipartResolver：使用Jakarta Commons FileUpload 解析 multipart 请求；
     * 一个是 StandardServletMultipartResolver：依赖于 Servlet 3.0 解析 multipart 请求；
     * Spring boot 默认使用 StandardServletMultipartResolver 解析 multipart 请求；
     */
    // 自定义 multipartResolver
    @Bean(name = "multipartResolver")
    public MultipartResolver multipartResolver() {
//        log.info("----------Loading the multipart resolver-------------");
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setDefaultEncoding("UTF-8");
        commonsMultipartResolver.setMaxUploadSize(3 * 1024 * 1024);
        return commonsMultipartResolver;
    }

    // 覆盖multipartResolver,将其添加到对应的multipartFilter,可以不用写
//    @Bean
//    @Order(0)
//    public MultipartFilter multipartFilter() {
//        MultipartFilter multipartFilter = new MultipartFilter();
//        multipartFilter.setMultipartResolverBeanName("multipartResolver");
//        return multipartFilter;
//    }
}
